$(function () {
    $("#backPage").load("/html/backPage.html");
})
// 查询自己的所有日报
function findAll() {
    let data = {
        userCode:userCode,
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data)
    let result = jsonGetValue("/logs/getMyLogsList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>"+ data[i].id +"</td>\n" +
            "        <td>"+ data[i].name +"</td>\n" +
            "        <td>"+ data[i].teacher +"</td>\n" +
            "        <td>"+ data[i].coach +"</td>\n" +
            "        <td>"+ data[i].content +"</td>\n" +
            "        <td>"+ data[i].gains +"</td>\n" +
            "        <td>"+ data[i].lack +"</td>\n" +
            "        <td>"+ data[i].logsState +"</td>\n" +
            "        <td>"+ data[i].createTime +"</td>\n";
        html += "    <td>\n" +
            "            <div onclick='check(" + data[i].id +")' title='查看'>" +
            '            <a href="#check-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-check">&#xe61c;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }

    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
    getInfo();
}

// 获取班主任和教练名
function getInfo() {
    let teamCode = $("#teamCode").text();
    let param = {
        teamCode: teamCode
    }
    let result = jsonGetValue("/team/getTeamByTeamCode", param);
    console.log(result);
    $("#addName").val($("#loginUser").text());
    $("#addTeacher").val(result.data.data.teacherName);
    $("#addCoach").val(result.data.data.coachName);
}
// 获取添加模态框的数据
function setAddLogs() {
    let userCode = sessionStorage.getItem("userCode");
    let name= $("#addName").val();
    let teacherName=  $("#addTeacher").val();
    let coachName= $("#addCoach").val();
    let content= $("#addContent").val();
    let gains= $("#addGains").val();
    let lack= $("#addLack").val();
    let data = {
        userCode: userCode,
        name: name,
        teacher: teacherName,
        coach: coachName,
        content: content,
        gains: gains,
        lack: lack
    };
    return data;
}

// 添加为草稿状态
function doSave() {
    let data = setAddLogs();
    console.log(data)
    let result = jsonPost("/logs/saveLogs", data);
    console.log(result);
    if (result.data.data == "success") {
        $(".right").load("/html/logs/my-logs.html");
    }
}
// 添加为提交状态
function doSubmit() {
    let data = setAddLogs();
    let result = jsonPost("/logs/submitLogs", data);
    console.log(result);
    if (result.data.data == "success") {
        $(".right").load("/html/logs/my-logs.html");
    }
}

// 修改为草稿状态
function doCheckSave() {
    let data = {id: $("#checkId").val()}
    console.log(data)
    let result = jsonPost("/logs/saveCheckLogs", data);
    console.log(result);
    if (result.data.data == 1) {
        $(".right").load("/html/logs/my-logs.html");
    }
}
// 修改为提交状态
function doCheckSubmit() {
    let data = {id: $("#checkId").val()}
    let result = jsonPost("/logs/submitCheckLogs", data);
    console.log(result);
    if (result.data.data == 1) {
        $(".right").load("/html/logs/my-logs.html");
    }
}

// 根据id获取日报信息
function getLogsById(id) {
    let data = {id:id}
    let result = jsonGetValue("/logs/getLogsById", data);
    console.log(result);
    setLogsDataById(result.data.data);
}

// 给查看模态框赋值
function setLogsDataById(data) {
    $("#checkId").val(data.id);
    $("#checkName").val(data.name);
    $("#checkCreateTime").val(data.createTime);
    $("#checkTeacher").val(data.teacher);
    $("#checkCoach").val(data.coach);
    $("#checkContent").val(data.content);
    $("#checkGains").val(data.gains);
    $("#checkLack").val(data.lack);
    if (data.stateCode == "submitted" || data.stateCode == "viewed"){
        let html = ' <a href="#close" title="关闭" class="modalBox-close-btn">\n' +
                   ' <input type="button" value="取消"></a>';
        $("#btnType").html(html);
    } else {
        let html = '<input type="button" value="保存" id="save" onclick="doCheckSave()">'+
                   '<input type="button" value="提交" id="submit" onclick="doCheckSubmit()">';
        $("#btnType").html(html);
    }
}

// 弹出查看模态框
function check(id) {
    getLogsById(id);
}

// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}